package seGobangClient.uiSystem3D;

import javax.media.j3d.Shape3D;
import javax.vecmath.*;
import javax.media.j3d.*;

public class Plane2D extends Shape3D {
	private float width, height;
	private TriangleStripArray Gem;
	private Appearance App;
	
	public Plane2D(float w, float h, Color3f color, float alpha, Texture texture) {
		width=w;
		height=h;
		Gem=new TriangleStripArray(4,QuadArray.COORDINATES|QuadArray.TEXTURE_COORDINATE_2,new int[]{4});		//��������
		Gem.setCoordinates(0, getVectexes());											//���붥�����
		Gem.setTextureCoordinates(0,0, getTexCoord());									//�����������
		
		App=new Appearance();
		
		TransparencyAttributes AppTA=new TransparencyAttributes();
			AppTA.setTransparency(alpha);
			AppTA.setTransparencyMode(TransparencyAttributes.NICEST);
		App.setTransparencyAttributes(AppTA);
		
		if (texture!=null){
			TextureAttributes AppTextureA=new TextureAttributes();
			AppTextureA.setTextureMode(TextureAttributes.MODULATE);
			App.setTextureAttributes(AppTextureA);
			App.setTexture(texture);
		}
		
		if (color!=null){
		ColoringAttributes AppCA=new ColoringAttributes();
			AppCA.setColor(color);
			App.setColoringAttributes(AppCA);
		}
		
		App.setPolygonAttributes(new PolygonAttributes
				(PolygonAttributes.POLYGON_FILL,PolygonAttributes.CULL_BACK,0));
		
		App.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
		App.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
		App.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
		
		this.setGeometry(Gem);
		this.setAppearance(App);
	}
	public void setColor(Color3f color){
		ColoringAttributes AppCA=new ColoringAttributes();
			AppCA.setColor(color);
		App.setColoringAttributes(AppCA);
	}
	
	public void setAlpha(float alpha){
		TransparencyAttributes AppTA=new TransparencyAttributes();
		AppTA.setTransparency(alpha);
		AppTA.setTransparencyMode(TransparencyAttributes.NICEST);
		App.setTransparencyAttributes(AppTA);
	}
	
	public void setTexture(Texture texture){
		if (texture!=null)
			App.setTexture(texture);
	}
	
	private Point3f [] getVectexes(){
		Point3f [] res={
				new Point3f((float)(-.5*width),(float)(.5*height),0f),	
				new Point3f((float)(-.5*width),(float)(-.5*height),0f),	
				new Point3f((float)(.5*width),(float)(.5*height),0f),	
				new Point3f((float)(.5*width),(float)(-.5*height),0f),	
		};
		return res;
	}
	private float [] getTexCoord(){
		float [] res={
				0f,1f,
				0f,0f,
				1f,1f,
				1f,0f
		};
		return res;
	}
}